package com.xiaour.spring.boot;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import java.util.Map;
import java.util.Properties;

@SpringBootApplication
//@ComponentScan("com.xiaour.spring.boot.controller")
//@EnableDiscoveryClient
@EnableDiscoveryClient
public class SpringBootDemoV3Application {

    private static final Logger log = LoggerFactory.getLogger(SpringBootDemoV3Application.class);

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoV3Application.class, args);
    }


    private static void initNacos() {
        // 初始化 NamingService（默认使用 gRPC 2.0）
        Properties properties = new Properties();
        properties.put("serverAddr", "82.156.29.215:8848");
        properties.put("namespace", "public"); // 支持多命名空间隔离
        NamingService naming = null;
        try {
            naming = NamingFactory.createNamingService(properties);
        } catch (NacosException e) {
            log.error("初始化 NamingService 失败", e);
            throw new RuntimeException(e);
        }

        // 注册服务实例（支持元数据扩展）
        Instance instance = new Instance();
        instance.setIp("2.156.29.215");
        instance.setPort(8080);
        instance.setServiceName("order-service");
        instance.setEphemeral(true); // 临时实例（默认）

        instance.setMetadata(Map.of("zone", "shanghai", "version", "v3","display_name","测试服务")); // 自定义标签
        try {
            naming.registerInstance("order-service", instance);
        } catch (NacosException e) {
            log.error("注册服务实例失败", e);
            throw new RuntimeException(e);
        }

        // 查询健康实例（按标签过滤）
//		List<Instance> instances = naming.selectInstances(
//				"order-service",
//				Collections.singletonMap("version", "v3"),
//				true
//		);

    }

    private static void initNacos2() {
        String serverAddr = "2.156.29.215:8848";


        Properties properties = new Properties();
//# 指定Nacos-Server的地址
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAddr);
//# 指定Nacos-SDK的命名空间
        properties.setProperty(PropertyKeyConst.NAMESPACE, "dev2");

//# 初始化配置中心的Nacos Java SDK
        try {
            ConfigService configService = NacosFactory.createConfigService(properties);

			configService.getConfig("com.xiaour.spring.boot.service.CatService", "DEFAULT_GROUP", 5000);
        } catch (NacosException e) {
            log.error("初始化配置中心失败", e);

            throw new RuntimeException(e);
        }

//# 初始化配置中心的Nacos Java SDK
        try {
            NamingService namingService = NacosFactory.createNamingService(properties);

        } catch (NacosException e) {
            log.error("初始化命名服务失败", e);

            throw new RuntimeException(e);
        }

//# 初始化分布式锁的Nacos Java SDK
//        try {
//            LockService lockService = NacosLockFactory.createLockService(properties);
//        } catch (NacosException e) {
//            log.error("初始化分布式锁失败", e);
//
//            throw new RuntimeException(e);
//        }
    }

}
